In [1]:
a = 6
b = 7
c = 42
print(1, a == 6)
print(2, a == 7)
print(3, a == 6 and b == 7)
print(4, a == 7 and b == 7)
print(5, not a == 7 and b == 7)
print(6, a == 7 or b == 7)
print(7, a == 7 or b == 6)
print(8, not (a == 7 and b == 6))
print(9, not a == 7 and b == 6)


1 True
2 False
3 True
4 False
5 True
6 True
7 False
8 True
9 False

In [2]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=a+%3D+6%0D%0Ab+%3D+7%0D%0Ac+%3D+42%0D%0Aprint(1,+a+%3D%3D+6)%0D%0Aprint(2,+a+%3D%3D+7)%0D%0Aprint(3,+a+%3D%3D+6+and+b+%3D%3D+7)%0D%0Aprint(4,+a+%3D%3D+7+and+b+%3D%3D+7)%0D%0Aprint(5,+not+a+%3D%3D+7+and+b+%3D%3D+7)%0D%0Aprint(6,+a+%3D%3D+7+or+b+%3D%3D+7)%0D%0Aprint(7,+a+%3D%3D+7+or+b+%3D%3D+6)%0D%0Aprint(8,+not+(a+%3D%3D+7+and+b+%3D%3D+6))%0D%0Aprint(9,+not+a+%3D%3D+7+and+b+%3D%3D+6)&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=12&codeDivWidth=350&codeDivHeight=400"> </iframe>



In [3]:
print(1, a == 6)
print(2, a == 7)


1 True
2 False

In [4]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=print(1,+a+%3D%3D+6)%0D%0Aprint(2,+a+%3D%3D+7)%0D%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400"> </iframe>



In [5]:
list = ["Life", "The Universe", "Everything", "Jack", "Jill", "Life", "Jill"]
 
# make a copy of the list. See the More on Lists chapter to explain what [:] means.
copy = list[:]
# sort the copy
copy.sort()
prev = copy[0]
del copy[0]
 
count = 0
 
# go through the list searching for a match
while count < len(copy) and copy[count] != prev:
    prev = copy[count]
    count = count + 1
 
# If a match was not found then count can't be < len
# since the while loop continues while count is < len
# and no match is found
 
if count < len(copy):
    print("First Match:", prev)


First Match: Jill

In [6]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=list+%3D+%5B%22Life%22,+%22The+Universe%22,+%22Everything%22,+%22Jack%22,+%22Jill%22,+%22Life%22,+%22Jill%22%5D%0D%0A+%0D%0A%23+make+a+copy+of+the+list.+See+the+More+on+Lists+chapter+to+explain+what+%5B%3A%5D+means.%0D%0Acopy+%3D+list%5B%3A%5D%0D%0A%23+sort+the+copy%0D%0Acopy.sort()%0D%0Aprev+%3D+copy%5B0%5D%0D%0Adel+copy%5B0%5D%0D%0A+%0D%0Acount+%3D+0%0D%0A+%0D%0A%23+go+through+the+list+searching+for+a+match%0D%0Awhile+count+%3C+len(copy)+and+copy%5Bcount%5D+!%3D+prev%3A%0D%0A++++prev+%3D+copy%5Bcount%5D%0D%0A++++count+%3D+count+%2B+1%0D%0A+%0D%0A%23+If+a+match+was+not+found+then+count+can't+be+%3C+len%0D%0A%23+since+the+while+loop+continues+while+count+is+%3C+len%0D%0A%23+and+no+match+is+found%0D%0A+%0D%0Aif+count+%3C+len(copy)%3A%0D%0A++++print(%22First+Match%3A%22,+prev)%0D%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400"> </iframe>


examples


In [8]:
## This program asks a user for a name and a password.
# It then checks them to make sure that the user is allowed in.
 
name = input("What is your name? ")
password = input("What is the password? ")
if name == "Josh" and password == "Friday":
    print("Welcome Josh")
elif name == "Fred" and password == "Rock":
    print("Welcome Fred")
else:
    print("I don't know you.")


What is your name? kwj
What is the password? 0428
I don't know you.

In [9]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=%23%23+This+program+asks+a+user+for+a+name+and+a+password.%0D%0A%23+It+then+checks+them+to+make+sure+that+the+user+is+allowed+in.%0D%0A+%0D%0Aname+%3D+input(%22What+is+your+name%3F+%22)%0D%0Apassword+%3D+input(%22What+is+the+password%3F+%22)%0D%0Aif+name+%3D%3D+%22Josh%22+and+password+%3D%3D+%22Friday%22%3A%0D%0A++++print(%22Welcome+Josh%22)%0D%0Aelif+name+%3D%3D+%22Fred%22+and+password+%3D%3D+%22Rock%22%3A%0D%0A++++print(%22Welcome+Fred%22)%0D%0Aelse%3A%0D%0A++++print(%22I+don't+know+you.%22)&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%22kwj%22,%22Rock%22%5D&curInstr=2&codeDivWidth=350&codeDivHeight=400"> </iframe>



In [ ]: